package com.neuedu.array;

/**
 * 数组的使用
 */
public class MyArray {

    public static void main(String[] args) {
        //
        // int age = 18;
        // int age1 = 18;
        // int age2 = 18;
        // int age3 = 18;
        // int age4 = 18;

        // 申请30 个  能够存放int 数据的 数组  一维数组的声明
        // int[]  ages = new int[30];

        // 初始化
        // int  companyAges[] = new int[]{18,18,19,20,22};

        int companyAges[] = {18, 18, 19, 20, 22};

        System.out.println("companyAges.length = " + companyAges.length);
        // 动态的创建
        int ages[] = new int[50];
        double salary = 99999.99;


        // 数组的访问，通过下标的形式 访问数组中的元素   ages[0--- length-1]

        ages[0] = 19;
        ages[1] = 18;
        ages[2] = 17;
        ages[3] = 20;


        // 获取数组中的元素
        System.out.println("ages[0] = " + ages[0]);
        System.out.println("ages[1] = " + ages[1]);
        System.out.println("ages[2] = " + ages[2]);
        System.out.println("ages[3] = " + ages[3]);


        // length 属性

        System.out.println("ages.length : " + ages.length);


        // 遍历数组


        for (int i = 0; i < ages.length; i++) {
            System.out.println("遍历数组  ages[" + i + "] = " + ages[i]);
        }


        // 数组有默认值   byte short int long  : 0   float double 0.0
        // int myAge;
        // System.out.println(myAge);
        double salarys[] = new double[30];
        boolean success[] = new boolean[30];
        char chars[] = new char[30];

        System.out.println("salarys[0] = " + salarys[0]);
        System.out.println("salarys[1] = " + salarys[1]);

        System.out.println("success[0] = " + success[0]);
        System.out.println("success[1] = " + success[1]);
        System.out.println("(int)chars[0] = " + (int) chars[0]);
        System.out.println("chars[0] = " + chars[0]);
        System.out.println("chars[1] = " + chars[1]);

        System.out.println("(int)'0'  " + (int) '0');
        System.out.println("(int)'a'  " + (int) 'a');


        // double ddd = 10;
        double salrys[] = {999.88, 777};

        System.out.println("salrys.length = " + salrys.length);
        System.out.println("salrys[0] = " + salrys[0]);
        System.out.println("salrys[1] = " + salrys[1]);
        /***
         * Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
         * 	at com.neuedu.array.MyArray.main(MyArray.java:85)
         *
         * 	ArrayIndexOutOfBoundsException ： 数组索引越界
         */
        // System.out.println("salrys[2] = " + salrys[2]);


        /**
         * Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 2
         * 	at com.neuedu.array.MyArray.main(MyArray.java:92)
         *
         * 	出现异常，后面的地面无法正常执行， 程序抛出异常， 抛出给 main的调用（jvm）的位置
         */
        // System.out.println("salrys[-1] = " + salrys[-1]);


        int unSortArray[] = {17, 25, 9, 12, 6, 4, 5, 18};

        for (int i = 0; i < unSortArray.length - 1; i++) {
            for (int j = 0; j < unSortArray.length - i-1; j++) {
                if (unSortArray[j] > unSortArray[j + 1]) {

                    // [j]   25   [j+1]  9
                    int temp = unSortArray[j];
                    unSortArray[j] = unSortArray[j+1];;
                    unSortArray[j+1] = temp;
                }
            }
        }

        System.out.println("数组排序之后的结果");
        for (int i = 0; i < unSortArray.length; i++) {
            System.out.println("unSortArray[i] = " + unSortArray[i]);
        }


    }
}
